<!DOCTYPE html>
<html prefix="og: http://ogp.me/ns#">
<head>
    <meta charset='utf-8'>
    <title>Fresco | 配置Image Pipeline</title>
    <link rel='stylesheet' href='/static/site.css' type='text/css'/>
    <link rel="stylesheet" href='/static/pygments.css' type='text/css'/>
    <link rel='shortcut icon' href='/static/favicon.png'>
    <meta name='viewport' content='width=480'>
    <meta property="og:title" content="配置Image Pipeline" />
    <meta property="og:site_name" content="fresco">
    <meta property='og:description' content='Fresco is an image management library.'>
    <meta property='og:image' content='http://facebook.github.io/fresco/static/fresco-og-image.png'>
    <meta property='og:url' content="http://facebook.github.io/fresco/docs/configure-image-pipeline.html">
    
    <meta property='og:type' content='website'>
    
    <script type="text/javascript" src="//use.typekit.net/vqa1hcx.js"></script>
    <script type="text/javascript">{'try{Typekit.load();}catch(e){}'}</script>
    <!-- Google Analytics -->
    <script>
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
      (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
      m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

      ga('create', 'UA-43024238-8', 'auto');
      ga('send', 'pageview');

    </script>
</head>
<body>

<header class='topbar'><nav class='width'>
<a href='/'>
  <img src="/static/fresco-logo.png" class="logo">
</a>
    <ul>
      <li><a href="/docs/index.html#_"
          
          class="active" >
          文档
      </a></li>
      <li><a href="/"
          >
          关于
      </a></li>
      <li><a href="/support.html"
          >
          支持
      </a></li>
      <li><a href="/javadoc">
          API
      </a></li>
      <li><a href="http://github.com/facebook/fresco">GitHub</a>
      <li><a href="/about.html"
          >
      关于本文档</a></li>
    </ul>
</nav></header>


<section class='content'><div class='width'>

<nav class='toc'>
  
    <section>
      <h3>配置和使用</h3>
      <ul>
        
          <li>
            <a href="/docs/download-fresco.html#_">
              下载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/index.html#_">
              配置和开始使用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/concepts.html#_">
              关键概念
            </a>
            
          </li>
        
          <li>
            <a href="/docs/supported-uris.html#_">
              支持的 URIs
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Drawee 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/using-drawees-xml.html#_">
              在XML中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-drawees-code.html#_">
              在Java代码中使用 Drawees
            </a>
            
          </li>
        
          <li>
            <a href="/docs/drawee-components.html#_">
              Drawee的各种效果配置
            </a>
            
          </li>
        
          <li>
            <a href="/docs/scaling.html#_">
              缩放
            </a>
            
          </li>
        
          <li>
            <a href="/docs/rounded-corners-and-circles.html#_">
              圆角/圆圈
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-controllerbuilder.html#_">
              使用ControllerBuilder
            </a>
            
          </li>
        
          <li>
            <a href="/docs/progressive-jpegs.html#_">
              渐进式JPEG图
            </a>
            
          </li>
        
          <li>
            <a href="/docs/animations.html#_">
              动画图(gif)
            </a>
            
          </li>
        
          <li>
            <a href="/docs/requesting-multiple-images.html#_">
              多图请求及图片复用
            </a>
            
          </li>
        
          <li>
            <a href="/docs/listening-download-events.html#_">
              监听下载事件
            </a>
            
          </li>
        
          <li>
            <a href="/docs/resizing-rotating.html#_">
              缩放和旋转
            </a>
            
          </li>
        
          <li>
            <a href="/docs/modifying-image.html#_">
              图片的修改
            </a>
            
          </li>
        
          <li>
            <a href="/docs/image-requests.html#_">
              图片请求
            </a>
            
          </li>
        
          <li>
            <a href="/docs/writing-custom-views.html#_">
              自定义View
            </a>
            
          </li>
        
          <li>
            <a href="/docs/gotchas.html#_">
              一些陷阱
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>Image Pipeline 指南</h3>
      <ul>
        
          <li>
            <a href="/docs/intro-image-pipeline.html#_">
              什么是Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/configure-image-pipeline.html#_" class="active">
              配置Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/caching.html#_">
              缓存的行为控制
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-image-pipeline.html#_">
              直接使用Image Pipeline
            </a>
            
          </li>
        
          <li>
            <a href="/docs/datasources-datasubscribers.html#_">
              数据源/订阅数据源
            </a>
            
          </li>
        
          <li>
            <a href="/docs/closeable-references.html#_">
              可关闭的引用
            </a>
            
          </li>
        
      </ul>
    </section>
  
    <section>
      <h3>第三方类库</h3>
      <ul>
        
          <li>
            <a href="/docs/using-other-network-layers.html#_">
              自定义网络加载
            </a>
            
          </li>
        
          <li>
            <a href="/docs/using-other-image-loaders.html#_">
              使用其他的Image Loader
            </a>
            
          </li>
        
      </ul>
    </section>
  
</nav>


<article class='withtoc'>
    <h1>
      配置Image Pipeline
      <a class="edit-page-link" href="https://github.com/liaohuqiu/fresco-docs-cn/blob/docs/docs/02-configure-image-pipeline.md" target="_blank">编辑和纠错</a>
    </h1>
    <p></p>

    <p>对于大多数的应用，Fresco的初始化，只需要以下一句代码:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Fresco</span><span class="o">.</span><span class="na">initialize</span><span class="o">(</span><span class="n">context</span><span class="o">);</span>
</code></pre></div>
<p>对于那些需要更多进一步配置的应用，我们提供了<a href="../javadoc/reference/com/facebook/imagepipeline/core/ImagePipelineConfig.html">ImagePipelineConfig</a>。</p>

<p>以下是一个示例配置，列出了所有可配置的选项。几乎没有应用是需要以下这所有的配置的，列出来仅仅是为了作为参考。</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">ImagePipelineConfig</span> <span class="n">config</span> <span class="o">=</span> <span class="n">ImagePipelineConfig</span><span class="o">.</span><span class="na">newBuilder</span><span class="o">()</span>
    <span class="o">.</span><span class="na">setBitmapMemoryCacheParamsSupplier</span><span class="o">(</span><span class="n">bitmapCacheParamsSupplier</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setCacheKeyFactory</span><span class="o">(</span><span class="n">cacheKeyFactory</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setEncodedMemoryCacheParamsSupplier</span><span class="o">(</span><span class="n">encodedCacheParamsSupplier</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setExecutorSupplier</span><span class="o">(</span><span class="n">executorSupplier</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setImageCacheStatsTracker</span><span class="o">(</span><span class="n">imageCacheStatsTracker</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setMainDiskCacheConfig</span><span class="o">(</span><span class="n">mainDiskCacheConfig</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setMemoryTrimmableRegistry</span><span class="o">(</span><span class="n">memoryTrimmableRegistry</span><span class="o">)</span> 
    <span class="o">.</span><span class="na">setNetworkFetchProducer</span><span class="o">(</span><span class="n">networkFetchProducer</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setPoolFactory</span><span class="o">(</span><span class="n">poolFactory</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setProgressiveJpegConfig</span><span class="o">(</span><span class="n">progressiveJpegConfig</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setRequestListeners</span><span class="o">(</span><span class="n">requestListeners</span><span class="o">)</span>
    <span class="o">.</span><span class="na">setSmallImageDiskCacheConfig</span><span class="o">(</span><span class="n">smallImageDiskCacheConfig</span><span class="o">)</span>
    <span class="o">.</span><span class="na">build</span><span class="o">();</span>
<span class="n">Fresco</span><span class="o">.</span><span class="na">initialize</span><span class="o">(</span><span class="n">context</span><span class="o">,</span> <span class="n">config</span><span class="o">);</span>
</code></pre></div>
<p>请记得将配置好的<code>ImagePipelineConfig</code> 传递给 <code>Fresco.initialize!</code> 否则仍旧是默认配置。</p>

<h3>关于Supplier</h3>

<p>许多配置的Builder都接受一个<a href="../javadoc/reference/com/facebook/common/internal/Supplier.html">Supplier</a> 类型的参数而不是一个配置的实例。</p>

<p>创建时也许有一些麻烦，但这带来更多的利好：这允许在运行时改变创建行为。以内存缓存为例，每隔5分钟就可检查一下Supplier，根据实际情况返回不同类型。</p>

<p>如果你需要动态改变参数，那就是用Supplier每次都返回同一个对象。</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Supplier</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;</span> <span class="n">xSupplier</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Supplier</span><span class="o">&lt;</span><span class="n">X</span><span class="o">&gt;()</span> <span class="o">{</span>
  <span class="kd">public</span> <span class="n">X</span> <span class="nf">get</span><span class="o">()</span> <span class="o">{</span>
    <span class="k">return</span> <span class="k">new</span> <span class="nf">X</span><span class="o">(</span><span class="n">xparam1</span><span class="o">,</span> <span class="n">xparam2</span><span class="o">...);</span>
  <span class="o">}</span>
<span class="o">);</span>
<span class="c1">// when creating image pipeline</span>
<span class="o">.</span><span class="na">setXSupplier</span><span class="o">(</span><span class="n">xSupplier</span><span class="o">);</span>
</code></pre></div>
<h3>线程池</h3>

<p>Image pipeline 默认有3个线程池:</p>

<ol>
<li>3个线程用于网络下载</li>
<li>两个线程用于磁盘操作: 本地文件的读取，磁盘缓存操作。</li>
<li>两个线程用于CPU相关的操作: 解码，转换，以及后处理等后台操作。</li>
</ol>

<p>对于网络下载，你可以定制网络层的操作，具体参考:<a href="using-other-network-layers.html">自定义网络层加载</a>.</p>

<p>对于其他操作，如果要改变他们的行为，传入一个<a href="../javadoc/reference/com/facebook/imagepipeline/core/ExecutorSupplier.html">ExecutorSupplier</a>即可。</p>

<h3>内存缓存的配置</h3>

<p>内存缓存和未解码的内存缓存的配置由一个Supplier控制，这个Supplier返回一个<a href="../javadoc/reference/com/facebook/imagepipeline/cache/MemoryCacheParams.html#MemoryCacheParams(int,%20int,%20int,%20int,%20int)">MemoryCacheParams</a> 对象用于内存状态控制。</p>

<h3>配置磁盘缓存</h3>

<p>你可使用Builder模式创建一个 <a href="../javadoc/reference/com/facebook/cache/disk/DiskCacheConfig.Builder.html">DiskCacheConfig</a>:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">DiskCacheConfig</span> <span class="n">diskCacheConfig</span> <span class="o">=</span> <span class="n">DiskCacheConfig</span><span class="o">.</span><span class="na">newBuilder</span><span class="o">()</span>
   <span class="o">.</span><span class="na">set</span><span class="o">....</span>
   <span class="o">.</span><span class="na">set</span><span class="o">....</span>
   <span class="o">.</span><span class="na">build</span><span class="o">()</span>

<span class="c1">// when building ImagePipelineConfig</span>
<span class="o">.</span><span class="na">setMainDiskCacheConfig</span><span class="o">(</span><span class="n">diskCacheConfig</span><span class="o">)</span>
</code></pre></div>
<h3>缓存统计</h3>

<p>如果你想统计缓存的命中率，你可以实现<a href="../javadoc/reference/com/facebook/imagepipeline/cache/ImageCacheStatsTracker.html">ImageCacheStatsTracker</a>, 在这个类中，每个缓存时间都有回调通知，基于这些事件，可以实现缓存的计数和统计。</p>


    <div class="docs-prevnext">
      
        <a href="/docs/intro-image-pipeline.html#_">&larr; 上一页</a>
      
      
        <a class="right" href="/docs/caching.html#_">下一页 &rarr;</a>
      
    </div>

    <a id="_"></a>
</article>

</div></section>
<footer><div class='width'>
    &copy; Copyright 2015, Facebook Inc.
</div></footer>

<script src="/static/linkify.js"></script>
<!-- Google Analytics -->
</body>
</html>

